$(document).ready(function() {
	$(document).ajaxError(function(){
        alert("服务运行时错误,请刷新后重试!");
        return;
    });
	//	资源状态展开，收起
	$("div.status .slide").toggle(function() {
		var id = $(this).parent().attr("id");
		$("#" + id + "_content").slideDown();
		$(this).attr("src", "images/status/slideup.gif");
	}, function() {
		var id = $(this).parent().attr("id");
		$("#" + id + "_content").slideUp();
		$(this).attr("src", "images/status/slidedown.gif");
	});
	
	//	详细页资源状态展示详细信息
	$("#status1_content .showDetail").click(function() {
		showStatusDetail($(this), $("#status1_content .showDetail"));
	});
	$("#status2_content .showDetail").click(function() {
		showStatusDetail($(this), $("#status2_content .showDetail"));
	});
	$("#status3_content .showDetail").click(function() {
		showStatusDetail($(this), $("#status3_content .showDetail"));
	});
	$("#status4_content .showDetail").click(function() {
		showStatusDetail($(this), $("#status4_content .showDetail"));
	});
	$("#status5_content .showDetail").click(function() {
		showStatusDetail($(this), $("#status5_content .showDetail"));
	});
	$("#status6_content .showDetail").click(function() {
		showStatusDetail($(this), $("#status6_content .showDetail"));
	});
	
	//	弹出框资源状态展示详细信息
	$("#popupDialog img.showDetail").click(function() {
		showStatusDetail($(this), $("#popupDialog img.showDetail"));
	});

	//	修改资源状态弹出框
	$("#popupDialog").dialog({
		autoOpen : false,
		modal : true,
		width : 1200,
		height : 500,
		title : "当前状态：<font color='red'>呈批中</font>",
		resizable : false,
		show : "blind",
		hide : "slide",
		buttons : {
			"确定修改" : function() {
				$("#popupDialog form")[0].submit();
				$(this).dialog("close");
			},
			"取消修改" : function() {
				$(this).dialog("close");
			}
		}
	});
	
	var stateTemp = parseInt($("#curStatus").val())-1;
	$("div.status .slide:eq("+stateTemp+")").trigger("click");
	
	$("img.delete").click(function() {
		if ($("#curStatus").val() == 4) {
			alert("该条记录已完成投放不可删除！");
			return;
		}
		if(confirm('确实要删除该条记录吗？'))
		{
			var proMedId = $(this).attr("name");
			$('#projMedId').val(proMedId);
			$('#castDetailstatus').val(1);
			document.updateForm.action="deleteCastDetail.action";
			document.updateForm.submit();
		}
		else
		{
			return false;
		}
		
	});
	
	
	
});

//	打开弹出框
function modifyStatus() {
	var state = $("#curStatus").val();
	var selectedIndex = 0;
	if (state == null || state == "") {
		alert("暂无当前状态数据！");
		return;
	} else if (state == 4) {
		$("select[name='newStatus']").children().remove();
		$("select[name='newStatus']").append("<option value='4'>完成投放</option><option value='5'>完成结算</option>");
	} else if (state == 5 || state == 6) {
		alert("此状态不可修改！");
		return;
	} else {
		$("select[name='newStatus']").children().remove();
		$("select[name='newStatus']").append("<option value='1'>预占</option>");
		$("select[name='newStatus']").append("<option value='2'>呈批中</option>");
		$("select[name='newStatus']").append("<option value='3'>占用</option>");
		$("select[name='newStatus']").append("<option value='4'>完成投放</option>");
		$("select[name='newStatus']").append("<option value='5'>完成结算</option>");
		$("select[name='newStatus']").append("<option value='6'>中止</option>");
		selectedIndex = parseInt(state)-1;
	}
	($("select[name='newStatus']")[0]).selectedIndex = selectedIndex;
	
	var titles = ["预占", "呈批中", "占用", "完成投放", "完成结算", "中止 "];
	var index = parseInt($("#curStatus").val())-1;
	$("#popupDialog").dialog({title : "当前状态：<font color='red'>"+titles[index]+"</font>"});
	$("#popupDialog .showDetail").attr("src", "images/status/arrow_right.gif");
	$("#popupDialog .content_right").hide();
	$("#popupDialog").dialog("open");
}

//资源状态展示详细信息
function showStatusDetail($obj, $objs) {
	var visible = $($obj).parents(".content_left").next().is(":visible");
	var detailImg = $($obj).attr("src");
	$objs.attr("src", "images/status/arrow_right.gif");
	if (detailImg == "images/status/arrow_right.gif") {
		visible = false;
	}
	if (visible) {
		$($obj).attr("src", "images/status/arrow_right.gif");
		$($obj).parent().parent().parent().parent().parent().next().hide();
	} else {
		$($obj).attr("src", "images/status/arrow_left.gif");
		var right = $($obj).parent().parent().parent().parent().parent().next();
		right.css("margin-top", $obj.parent()[0].offsetTop);
		var params = {'idForMed' : $($obj).attr('name'),'projMedId':$($obj).attr('id'),'nowStatus':$($obj).attr('title'),'curStatus':$("#curStatus").val(),time: new Date().getTime()};
		$.getJSON("detailForMedium.action", params , function(data){
			right.empty();
			$('#value1').text(data.mediasumm.mediaName);
			$('#value2').text(data.mediumStandard);
			$('#value3').text(data.mediumStyle);
			$('#value4').text(data.mediumColumn);
			$('#value5').text(data.mediumPage);
			$('#value6').text(data.mediumPosition);
			$('#value7').text(data.mediumPrice);
			$('#value8').text(data.mediumAbility);
			
			var newElement = $("#detail_Med table").clone(true);
			
			
			//alert($(newElement.children()).children('tr:eq(1)').html());
			$(newElement.children()).children('tr:eq(1)').children('td')[0].id = "mediaName";
			$(newElement.children()).children('tr:eq(1)').children('td')[1].id = "mediumStandard";
			$(newElement.children()).children('tr:eq(1)').children('td')[2].id = "mediumFormat";
			$(newElement.children()).children('tr:eq(1)').children('td')[3].id = "mediumPage";
			
			$(newElement.children()).children('tr:eq(3)').children('td')[0].id = "mediumColumn";
			$(newElement.children()).children('tr:eq(3)').children('td')[1].id = "mediumPosition";
			$(newElement.children()).children('tr:eq(3)').children('td')[2].id = "mediumPrice";
			$(newElement.children()).children('tr:eq(3)').children('td')[3].id = "mediumAbility";
			right.append(newElement);
			right.show();
        });
	}
}












